//
//  BaseViewController.m
//  YUXiu
//
//  Created by 王竞成 on 16/6/22.
//  Copyright © 2016年 Wang. All rights reserved.
//

#import "BaseViewController.h"
#import "UINavigationBar+ChanheAlpha.h"

@interface BaseViewController (){

}

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self autoConfigViews];
    self.view.backgroundColor = kNaviBarCOLOR;
}

/**
 * @brief 设置navigationBar的背景颜色为透明
 */
-(void)setNavigationBarClearColor
{
    //设置navigationBar的背景颜色为透明
    [self.navigationController.navigationBar Transparent];
}
- (void)autoConfigViews {
    
}

- (void)selfConfigViews {
    
}

#pragma mark - UI操作
/**
 * @brief 设置导航栏右边的item
 * @param arr 按钮的图片数组
 */
- (void)setNavigationItemS:(NSArray<NSDictionary *> *)arr andIsLeftNavigationItem:(BOOL)isLeftNavigationItem
{
    
    NSMutableArray * arr_item = [[NSMutableArray alloc] init];
    //创建一个空的item，移动item的位置
    UIBarButtonItem *item_empty = [[UIBarButtonItem alloc] init];
    item_empty.width = -10;
    [arr_item addObject:item_empty];
    
    for (int i = 0; i < [arr count]; i ++)
    {
        NSDictionary * dic = arr[i];
        UIImage *img_normal = [UIImage imageNamed:dic[kNormeImage]];
        UIImage *img_select = [UIImage imageNamed:dic[kSelectImage]];
        
        //创建button
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTag:10000 + i];
        [btn setFrame:CGRectMake(0 , 0, img_normal.size.width, img_normal.size.height)];
        
        [btn setBackgroundImage:img_normal forState:UIControlStateNormal];
        [btn setBackgroundImage:img_select forState:UIControlStateSelected];
        if (isLeftNavigationItem)
        {
            [btn addTarget:self action:@selector(btnAction_leftBarButtonItem:) forControlEvents:UIControlEventTouchUpInside];
        }
        else
        {
            [btn addTarget:self action:@selector(btnAction_rightBarButtonItem:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        //创建item
        UIBarButtonItem * item_barButton = [[UIBarButtonItem alloc] initWithCustomView:btn];
        
        //添加到数组中
        [arr_item addObject:item_barButton];
        
    }
    if (isLeftNavigationItem)
    {
        [self.navigationItem setLeftBarButtonItems:arr_item];
    }
    else
    {
        //        [self.navigationItem setRightBarButtonItems:arr_item animated:YES];
        self.navigationItem.rightBarButtonItems = arr_item;
    }
    
}


#pragma mark - 导航栏点击事件
/**
 * @brief 右边barbuttonItem的点击事件
 */
-(void)btnAction_rightBarButtonItem:(UIButton *)sender
{
    
}

/**
 * @brief 左边barbuttonItem的点击事件
 */

-(void)btnAction_leftBarButtonItem:(UIButton *)sender
{
    
}

#pragma mark - HUD操作
- (void)showHUDWithInfo:(NSString *)info AfterDelay:(NSTimeInterval)delyay {
    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:kKeyWindow animated:YES];
    HUD.mode = MBProgressHUDModeIndeterminate;
    HUD.labelText = info;
    if (delyay == 0) {
        delyay = 1.f;
    }
    [HUD hide:YES afterDelay:delyay];
}

-(void)showHUDForFavoritByInfo:(NSString *)str AsImageName:(NSString *)imgName AfterDelay:(NSTimeInterval)delay{
    //error_new,success_new
    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:kKeyWindow animated:YES];
    HUD.mode = MBProgressHUDModeCustomView;
    UIImageView *imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imgName]];
    HUD.customView = imgV;
    HUD.labelText = str;
    if (delay == 0) {
        delay = 1.f;
    }
    [HUD hide:YES afterDelay:delay];
}

- (void)showHUDWithText:(NSString *)info AfterDelay:(NSTimeInterval)delyay{
    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:kKeyWindow animated:YES];
    HUD.mode = MBProgressHUDModeText;
//    HUD.labelText = info;
    HUD.detailsLabelText = info;

    if (delyay == 0) {
        delyay = 1.f;
    }
    [HUD hide:YES afterDelay:delyay];
    

}


#pragma mark - keyboard Notification
-(void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShow:)
                                                 name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
    
}

-(void)keyboardWasShow:(NSNotification *)aNotification
{
    
}

-(void)keyboardWasHidden:(NSNotification *)aNotification
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
